<?php echo "<?php\n"; ?>

class <?php echo $this->controllerClass; ?> extends <?php echo $this->baseControllerClass."\n"; ?>
{
    //public $layout='//layouts/column2';

    public function filters()
    {
        return array(
            'rights', // perform access control for CRUD operations
        );
    }

    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new <?php echo $this->modelClass; ?>;

        if(isset($_POST['<?php echo $this->modelClass; ?>']))
        {
            $event = (int)$_POST['event'];
            $model->attributes=$_POST['<?php echo $this->modelClass; ?>'];

            if($model->save()){
                if ($event == 0){
                    $this->setNotice(
                        array(
                            array(
                            'title' => Yii::t('system', '<?php echo $this->modelClass; ?>'),
                            'text'  => Yii::t('system', 'Сохранено'),
                            'type'  => 'success'
                            ),
                        ));
                    $this->redirect(array('view','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>));
                }
                else {
                    $this->setNotice(
                                  array(
                                    array(
                                    'title' => Yii::t('system', '<?php echo $this->modelClass; ?>'),
                                    'text'  => Yii::t('system', 'Сохранено'),
                                    'type'  => 'success'
                                    ),
                                    array(
                                    'title' => Yii::t('system', '<?php echo $this->modelClass; ?>'),
                                    'text'  => Yii::t('system', 'Можете продолжить дополнение.'),
                                    'type'  => 'info'
                                    ),
                                  )
                            );
                      $this->redirect(array('create'));  
                }
            }

        }

        $this->render('create',array(
                'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        if(isset($_POST['<?php echo $this->modelClass; ?>']))
        {
                $model->attributes=$_POST['<?php echo $this->modelClass; ?>'];
                if($model->save()){
                    $this->setNotice(
                       array(
                            array(
                            'title' => Yii::t('system', '<?php echo $this->modelClass; ?>'),
                            'text'  => Yii::t('system', 'Информация успешно обновлена.'),
                            'type'  => 'success'
                            ),
                       ));
                    $this->redirect(array('view','id'=>$model-><?php echo $this->tableSchema->primaryKey; ?>));
                }
        }

        $this->render('update',array(
                'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();

            if(!isset($_GET['ajax']))
                    $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('<?php echo $this->modelClass; ?>');
        $this->render('index',array(
                'dataProvider'=>$dataProvider,
        ));
    }

    public function actionAdmin()
    {
        $model=new <?php echo $this->modelClass; ?>('search');
        $model->unsetAttributes();
        if(isset($_GET['<?php echo $this->modelClass; ?>']))
                $model->attributes=$_GET['<?php echo $this->modelClass; ?>'];

        $this->render('admin',array(
                'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=<?php echo $this->modelClass; ?>::model()->findByPk($id);
        if($model===null)
                throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='<?php echo $this->class2id($this->modelClass); ?>-form')
        {
                echo CActiveForm::validate($model);
                Yii::app()->end();
        }
    }
}